% extracts the data from the excell files, converts the values of the
% labels from pixeis to coordinates as worm position, normalizes the
% position in relation to a defined position for the OP stimuli so that all
% worms have the stimulus torwards they are heading in the same place

%OP stimuli = x=17500 y=12500

%% extra functions 
%convertx
%converty

%%
% DON'T FORGET TO CHANGE THE NAME OF THE FOLDER IN THE END;
%%

load ('C:\Users\Ana\Dropbox\Matlab\data\labels.mat');

% select the excell file from where to extract the data
[filename, pathname] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},'File Selector');


% stores the data from the excell file 
store = cell (1,30,1);

number = xlsread (filename,1);
time = xlsread (filename ,2);
xpos = xlsread (filename,3);
ypos = xlsread (filename,4);
speed = xlsread (filename,5);
[num, text, raw] = xlsread (filename,6);
reversals = raw (:,1);
[num1, text1, raw1] = xlsread (filename,7);
omegas = raw1 (:,1);
head_bending = xlsread (filename,8);
body_bending = xlsread (filename,9);
label_x = xlsread (filename,10);
label_y = xlsread (filename,11);
time_to_arrive = xlsread (filename,12);
tamanho = xlsread (filename,13);
conversion = xlsread (filename,14);

store {1,1,1} = number;
store {1,2,1} = time;
store {1,3,1} = xpos;
store {1,4,1} = ypos;
store {1,5,1} = speed;
store {1,6,1} = reversals;
store {1,7,1} = omegas;
store {1,8,1} = head_bending;
store {1,9,1} = body_bending;
store {1,10,1} = label_x;
store {1,11,1} = label_y;
store {1,12,1} = time_to_arrive;
store {1,13,1} = tamanho;
store {1,14,1} = conversion;
%%
%transform the value of the labels from pixeis to coordinates and store on
%the cell array

xlabpix = store {1,10,1};
ylabpix = store {1,11,1};
conver = store {1,14,1};

xlab = convertx (xlabpix,conver);
ylab = converty (ylabpix, conver);

store {1,15,1} = xlab;
store {1,16,1} = ylab;

%% find the parameters to align to the OP stimuli
%set the same value for the position of the OP stimuli - I decided these
%values in an arbitrary way to fit well in the graph
OPx = 17500; % this is the new value for the stimulus; same for all
OPy = 12500;

%find the distance between where the stimulus is in my video and the specificed OP stimuli
xdistOP = OPx - xlab;
ydistOP = OPy - ylab;

%% correct the values of x and y torwards the new stimulus values
hor = store (1,3,1);% to extract data from array
horvalues = hor {1,1};
n = size (horvalues, 1);
horfinal = [];%for x values horizontally
for i = 1:n;
    hornorm = horvalues (i) + xdistOP;
    a = hornorm;
    horfinal = [horfinal; a];
end

ver = store (1,4,1);% to extract data from array
vervalues = ver {1,1};
verfinal = [];%for y values vertically
for i = 1:n;
   vernorm = vervalues (i) + ydistOP;
    b = vernorm;
    verfinal = [verfinal; b];
end

store {1,17,1} = horfinal;
store {1,18,1} = verfinal;
store {1,23,1} = OPx;
store {1,24,1} = OPy;

% save the data in the designated file
MT_STM14_OP_vs_PA_PA8_Choice_OP_F1 = store ;

